<%@page import="util.Utilities"%>
<%@page import="dao.Examination"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
		    + request.getServerName() + ":" + request.getServerPort()
		    + path + "/";
%>
<%
    List<Examination> item1 = (List<Examination>) session
		    .getAttribute("item1");
    List<Examination> item2 = (List<Examination>) session
		    .getAttribute("item2");
    List<Examination> item3 = (List<Examination>) session
		    .getAttribute("item3");    
    List<String> answers1 = null, answers2 = null, answers3 = null;
    int counts1 = 0,counts2 = 0,counts3 = 0;
    if(item1 !=null)
	counts1 = item1.size();
    if(item2 !=null)
	counts2 = item2.size();
    if(item3 !=null)
	counts3 = item3.size();
    int[] isRight = new int[counts1];
    if (counts1 > 0) {
		answers1 = new ArrayList<String>();
		for (int i = 0; i < counts1; i++) {
		    String[] answer = request.getParameterValues("option" + (i+1));
		    String idString = request.getParameter("aid" + (i+1));
		    StringBuilder strs = new StringBuilder();
		    isRight[i] = Utilities.isRight(answer, item1.get(i)
			    .getAnswer());
		    for (String str : answer) {
			strs.append(str).append(",");
		    }
		    strs.replace(strs.length() - 1, strs.length(), "");
		    //ids1.add(Integer.parseInt(idString));
		    answers1.add(strs.toString());
		}
    }
    if (counts2 > 0) {
		answers2 = new ArrayList<String>();
		for (int i = 0; i < counts2; i++) {
		    String answer = request.getParameter("bAnswer" + (i+1));
		    String idString = request.getParameter("bid" + (i+1));
		    //ids2.add(Integer.parseInt(idString));
		    answers2.add(answer);
		}
    }
    if (counts3 > 0) {
		answers3 = new ArrayList<String>();
		for (int i = 0; i < counts3; i++) {
		    String answer = request.getParameter("cAnswer" + (i+1));
		    String idString = request.getParameter("cid" + (i+1));
		    answers3.add(answer);
		}
    }
    List<Integer> item1Ids = new ArrayList<Integer>();
    if(counts1 > 0)
    for(Examination exa:item1){
		item1Ids.add(exa.getExaminationid());
    }
    session.setAttribute("item1Ids", item1Ids);
    session.setAttribute("isRight1", isRight);
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>测试结果</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
<style type="text/css">
body {
	font: normal 11px auto "Trebuchet MS", Verdana, Arial, Helvetica,
		sans-serif;
	color: #4f6b72;
	text-align: center;
	background: #E6EAE9;
	width: 85%;
}


#container {
	margin-left: auto;
	margin-right: auto;
	width: 800px;
	height: 700px;
	background: #f8f8f8;
	display: block;
	vertical-align: middle;
}

.ele {
	margin-top: 15px;
	margin-botton: 15px;
	text-align: center;
	width: 100%;
	display: block;
	vertical-align: middle;
}
</style>
<script type="text/javascript">
function back(){
	resultForm.action = "./servlet/HandinExaminationServlet?type=back"
	resultForm.submit();
}
function go(){
	resultForm.action = "./servlet/HandinExaminationServlet?type=go"
		resultForm.submit();
}
</script>
</head>

<body>
	<div id="container">
		<form name="resultForm" method="post"
			action="">
			<div class="ele">
				<label>一.选择题结果：</label><br>	
				<input type="hidden" name="aCounts" id="aCounts"
							value="<%=counts1 %>">			
				<c:choose>
					<c:when test="<%=counts1 == 0 %>">
						<c:out value="没有相应的题目"></c:out>
					</c:when>
					<c:otherwise>					
						<%
						    int sequence = 1;
										for(Examination examination:item1){
										    StringBuilder builder = new StringBuilder(examination.getQuestion());
										    int examinationid = examination.getExaminationid();
						%>
						<c:choose>
							<c:when test="<%=builder.equals("img") %>">
								<img alt="" src=".<%=examination.getQuestionpath()%>">
							</c:when>
							<c:otherwise>
								<%
								    int point = builder.indexOf("@@@");
												    String stem = builder.substring(0, point);
												    int i = 0;
												    point = point + 3;
												    int point2 = builder.indexOf("##",point);
												    ArrayList<String> optionList = new ArrayList<String>(); 
												    while(i < 4){
														optionList.add(builder.substring(point,point2));
														i++;
														point = point2 + 2;
														point2 = builder.indexOf("##", point);
														if(point2 == -1){
														    point2 = builder.length();
														}
												    }
								%>
								<span><%=sequence%>.<%=stem%>( )</span>
								<br>
								<span>A.<%=optionList.get(0)%></span>
								<br>
								<span>B.<%=optionList.get(1)%></span>
								<br>
								<span>C.<%=optionList.get(2)%></span>
								<br>
								<span>D.<%=optionList.get(3)%></span>
								<br>
							</c:otherwise>
						</c:choose>
						<input type="hidden" name="aid<%=sequence%>"
							id="aid<%=sequence%>" value="<%=examinationid%>">
						<span>你的答案：<%=answers1.get(sequence-1)%></span>&nbsp;&nbsp;&nbsp;&nbsp;
				   <c:choose>
							<c:when test="<%=isRight[sequence-1] == 1 %>">
								<img alt="" src="./image/right.jpg">
							</c:when>
							<c:when test="<%=isRight[sequence-1] == 0 %>">
								<img alt="" src="./image/right1.JPG">
							</c:when>
							<c:otherwise>
								<img alt="" src="./image/wrong.jpg">
							</c:otherwise>
						</c:choose>
						<br>
						<c:if test="<%=isRight[sequence-1] != 1 %>">
							<br><span>正确答案：<%=examination.getAnswer()%></span>
						</c:if>
						<br><span>答案分析：</span>
						<c:choose>
						<c:when test="<%=examination.getAnalysispath() !=null %>">
						<img alt="" src=".<%=examination.getAnalysispath() %>">
						</c:when>
						<c:otherwise>
						<%=examination.getAnalysis()%>
						</c:otherwise>
						</c:choose>						
						<br>
						<%
						    sequence ++;
										}
						%>
					</c:otherwise>
				</c:choose>
			</div>
			<div class="ele">
				<label>二.填空题结果：</label><br>
				<input type="hidden" name="bCounts" id="bCounts"
							value="<%=counts2%>">
				<c:choose>
					<c:when test="<%=counts2 == 0 %>">
						<c:out value="没有相应的题目"></c:out>
					</c:when>
					<c:otherwise>
						<%
						    int sequence = 1;
										int spaceNumber = 1;
										for(Examination examination:item2){
										    String question = examination.getQuestion();
										    int examinationid = examination.getExaminationid();
						%>
						<c:choose>
							<c:when test="<%=question.equals("img") %>">
								<img alt="" src=".<%=examination.getQuestionpath()%>">
							</c:when>
							<c:otherwise>
								<%
								    question.replaceAll("&nbsp", "_______");
												    
								%>
								<span><%=sequence%>.<%=question%></span>
								<br>
							</c:otherwise>
						</c:choose>
						<input type="hidden" name="bid<%=sequence%>"
							id="bid<%=sequence%>" value="<%=examinationid%>">
						<span>你的答案：<%=answers2.get(sequence-1)%></span>
						<br>
						<span>正确答案：<%=examination.getAnswer()%></span>
						<br>
				   	正确<input type="radio" name="bIsRight<%=sequence%>" value="1">
				   	错误<input type="radio" name="bIsRight<%=sequence%>" value="0">
						<br><span>答案分析：</span>
						<c:choose>
						<c:when test="<%=examination.getAnalysispath() !=null %>">
						<img alt="" src=".<%=examination.getAnalysispath() %>">
						</c:when>
						<c:otherwise>
						<%=examination.getAnalysis()%>
						</c:otherwise>
						</c:choose>	
						<br>
						<%
						    sequence ++;
										}
						%>
					</c:otherwise>
				</c:choose>
			</div>
			<div class="ele">
				<label>三.解答题结果：</label><br>
				<input type="hidden" name="cCounts" id="cCounts"
							value="<%=counts3%>">
				<c:choose>
					<c:when test="<%=counts3 == 0 %>">
						<c:out value="没有相应的题目"></c:out>
					</c:when>
					<c:otherwise>
						<%
						    int sequence = 1;
										for(Examination examination:item3){
										    String question = examination.getQuestion();
										    int examinationid = examination.getExaminationid();
						%>
						<c:choose>
							<c:when test="<%=question.equals("img") %>">
								<img alt="" src=".<%=examination.getQuestionpath()%>">
							</c:when>
							<c:otherwise>
								<span><%=sequence%>.<%=question%></span>
								<br>
							</c:otherwise>
						</c:choose>
						<input type="hidden" name="cid<%=sequence%>"
							id="cid<%=sequence%>" value="<%=examinationid%>">
						<span>你的答案：<%=answers3.get(sequence-1)%></span>
						<br>
						<span>答案提示：<%=examination.getAnswer()%></span>
						<br>
				   	正确<input type="radio" name="cIsRight<%=sequence%>" value="1">
				   	错误<input type="radio" name="cIsRight<%=sequence%>" value="0">
						<%
						    sequence ++;
										}
						%>
					</c:otherwise>
				</c:choose>
			</div>
			<div class="ele">
				<input type="button" onclick="go()" value="继续做题">&nbsp;&nbsp;&nbsp;&nbsp; 
				<input type="button" onclick="back()" value="返回章节学习">
			</div>
		</form>
	</div>
</body>
</html>
